<?php

/**
 * This is the model class for table "comments".
 *
 * The followings are the available columns in table 'comments':
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property string $email
 * @property string $content
 * @property integer $parent_id
 * @property integer $post_id
 * @property integer $status
 * @property string $create_at
 *
 * The followings are the available model relations:
 * @property Announcement $post
 * @property Users $user
 * @property Comments $parent
 * @property Comments[] $comments
 */
class Comments extends CommentsBase
{
	public $title_post;
	public $count_comment;
	public $name;
	public $email;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'comments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('post_id, create_at, content', 'required'),
			array('user_id, parent_id, post_id, status', 'numerical', 'integerOnly'=>true),
			array('name, email', 'length', 'max'=>255),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, email, content, status, create_at, title_post', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'post' => array(self::BELONGS_TO, 'Announcement', 'post_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'parent' => array(self::BELONGS_TO, 'Comments', 'parent_id'),
			'reply' => array(self::HAS_MANY, 'Comments', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'Thành viên',
			'name' => 'Thành viên',
			'email' => 'Email',
			'content' => 'Nội dung',
			'parent_id' => 'Parent',
			'post_id' => 'Post',
			'status' => 'Tình trạng',
			'create_at' => 'Ngày đăng',
			'title_post' => 'Thông báo',
			'count_comment' => 'Trả lời'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($parentID = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('post', 'user', 'user.member', 'user.profile');
		$criteria->together = true;

		if ($parentID !== null) {
			$criteria->compare('parent_id',$parentID, false);
		}
		else {
			$criteria->condition ='parent_id is null';
		}
		if ($this->name != "") {
			$criteria->addCondition ("(member.fullname like '%" . $this->name . "%' OR profile.fullname like '%" . $this->name . "%')");
		}
		if ($this->email != "") {
			$criteria->addCondition ("(user.email like '%" . $this->email . "%')");
		}
		$criteria->compare('t.content',$this->content,true);
		$criteria->compare('t.status',$this->status);
		$criteria->compare('t.create_at',$this->create_at,true);
		$criteria->compare('post.title',$this->title_post,true);

		$sort = new CSort;
		$sort->attributes = array(
			'create_at' => array(
				'asc' => 'create_at',
				'desc' => 'create_at DESC',
			), '*', /* Treat all other columns normally */
		);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>$sort
		));
	}

	/**
	 * countCommentByParentId
	 */
	public function countCommentByParentId($parent_id)
	{
		return $this->model()->count("parent_id = " . $parent_id);
	}

	/**
	 * countCommentByPostId
	 */
	public function countCommentByPostId($post_id)
	{
		return $this->model()->count("post_id = " . $post_id);
	}

	/**
	 * countNewCommentByPostId
	 */
	public function countNewCommentByPostId($post_id)
	{
		return $this->model()->count("post_id = " . $post_id . " and highlight_mod = 1");
	}

	/**
	 * getCommentByPostId
	 */
	public function getCommentByPostId($post_id)
	{
		return $this->model()->findAll(array("condition" => "post_id = " . $post_id, "order" => "create_at DESC"));
	}
}